# coding=utf-8
from bs4 import BeautifulSoup
import re


class ResultCode():
    def __init__(self, file_path):
        self.file_path = file_path
        soup = BeautifulSoup(open(self.file_path, encoding='utf-8'), features='html.parser')
        all_number = soup.find_all('script', type='text/javascript')[1].string
        self.testAll = re.compile(r'''"testAll": (.+?),''').search(all_number).group(1)
        self.testPass = re.compile(r'''"testPass": (.+?),''').search(all_number).group(1)
        self.testFail = re.compile(r'''"testFail": (.+?),''').search(all_number).group(1)
        self.beginTime = re.compile(r'''"beginTime": "(.+?)",''').search(all_number).group(1)


    def error_rate(self):
        try:
            return ("错误率是：%.2f%%" % (int(self.testFail)/int(self.testAll)*100))
        except Exception as e:
            print(e)

    def success_rate(self):
        try:
            return ("正确率是：%.2f%%" % (int(self.testPass)/int(self.testAll)*100))
        except Exception as e:
            print(e)

    def error_code(self):
        try:
            num = int(self.testFail)/int(self.testAll)
            return num
        except:
            num = None
            return num

    def success_code(self):
        try:
            num = int(self.testPass)/int(self.testAll)
            return num
        except:
            num = None
            return num




if __name__ == '__main__':
    file_path = r"E:\TestAPIPython\perapitest\report\2020-09-04 11-14-11.html"
    a = ResultCode(file_path)
    print(a.error_code())